package cn.livingCloud.entity.core;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.Config;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.CaseCategoryMapper;
import cn.livingCloud.repository.CaseImageMapper;
import cn.livingCloud.repository.EmployeeMapper;
import cn.livingCloud.repository.MemberMapper;
import cn.livingCloud.utils.UuidGenerator;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 案件
 */
public class Case extends BaseEntity {
    private transient CaseImageMapper imageMapper = BeanProvider.getBean(CaseImageMapper.class);
    private transient MemberMapper memberMapper = BeanProvider.getBean(MemberMapper.class);
    private transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    private transient CaseCategoryMapper caseCategoryMapper = BeanProvider.getBean(CaseCategoryMapper.class);

    private String uuid = UuidGenerator.generate();

    private String creatorUuid;

    private String addressDescription;

    private String phone;

    private Float locationX;

    private Float locationY;

    private CaseStatus status = CaseStatus.NO_HANDLE;

    private String ownerId;

    private String caseCategoryUuid;

    private CaseStatus disableStatus;

    public Case() {
    }

//    public Case(CaseFormDto formDto) {
//        this.creatorUuid = formDto.getUuid();
//        this.addressDescription = formDto.getAddressDescription();
//    }

    public String getUuid() {
        return uuid;
    }

    public Case setUuid(String uuid) {
        this.uuid = uuid;
        return this;
    }

    @Override
    public Boolean getArchive() {
        return archive;
    }

    @Override
    public Case setArchive(Boolean archive) {
        this.archive = archive;
        return this;
    }

    @Override
    public Date getCreateDateTime() {
        return createDateTime;
    }

    @Override
    public Case setCreateDateTime(Date createDateTime) {
        this.createDateTime = createDateTime;
        return this;
    }

    public String getCreatorUuid() {
        return creatorUuid;
    }

    public Case setCreatorUuid(String creatorUuid) {
        this.creatorUuid = creatorUuid;
        return this;
    }

    public String getAddressDescription() {
        return addressDescription;
    }

    public Case setAddressDescription(String addressDescription) {
        this.addressDescription = addressDescription;
        return this;
    }

    public String getPhone() {
        return phone;
    }

    public Case setPhone(String phone) {
        this.phone = phone;
        return this;
    }

    public Float getLocationX() {
        return locationX;
    }

    public Case setLocationX(Float locationX) {
        this.locationX = locationX;
        return this;
    }

    public Float getLocationY() {
        return locationY;
    }

    public Case setLocationY(Float locationY) {
        this.locationY = locationY;
        return this;
    }

    public CaseStatus getStatus() {
        return status;
    }

    public void setStatus(CaseStatus status) {
        this.status = status;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public Case setOwnerId(String ownerId) {
        this.ownerId = ownerId;
        return this;
    }

    public CaseStatus getDisableStatus() {
        return disableStatus;
    }

    public Case setDisableStatus(CaseStatus disableStatus) {
        this.disableStatus = disableStatus;
        return this;
    }

    public List<Employee> getOwnerList() {
        if (StringUtils.isEmpty(ownerId)) {
            return new ArrayList<>();
        }
        List<String> owners = Arrays.asList(this.ownerId.split(","));
        if (owners.size() == 0) {
            return new ArrayList<>();
        }

        return employeeMapper.findEmployees(owners);
    }

    public String getCaseCategoryUuid() {
        return caseCategoryUuid;
    }

    public void setCaseCategoryUuid(String caseCategoryUuid) {
        this.caseCategoryUuid = caseCategoryUuid;
    }

    public Member getMember() {
        return memberMapper.findMemberByUuid(this.creatorUuid);
    }

    @Deprecated
    public Employee getEmployee() {
        return employeeMapper.selectByPrimaryKey(this.ownerId);
    }

    public CaseCategory getCaseCategory() {
        return caseCategoryMapper.selectByPrimaryKey(this.caseCategoryUuid);
    }

    public List<CaseImage> getAllCaseImages() {
        return imageMapper.findAllImagesByCaseUuid(this.uuid);
    }


    /**
     * 获取案件详情的url
     *
     * @return http://city.scyunju.com/front/cmc/case/info.htm?uuid=sddfd
     */
    public String getCaseInfoUrl() {
        String domainUrl = Config.getDomainUrl();
        return domainUrl + "front/cmc/case/info.htm?uuid=" + this.uuid;
    }
}